﻿using CommunityToolkit.Mvvm.ComponentModel;
using GameView.Models;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;

namespace GameView.Services.Comment
{
    public partial class MenuService : ObservableObject
    {
        private readonly HttpClient _httpClient;

        [ObservableProperty]
        private bool _isLoading;

        public MenuService()
        {
            _httpClient = new HttpClient
            {
                BaseAddress = new Uri(APIHelper.Menu)
            };
        }

        public async Task<ApiResponse<List<MenuEntity>>> GetAllAsync()
        {
            try
            {
                IsLoading = true;
                if (!string.IsNullOrEmpty(StaticValues.Token))
                    _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", StaticValues.Token);
                else
                    return default;
                var resp = await _httpClient.GetAsync("all");
                var apiResponse = await resp.Content.ReadFromJsonAsync<ApiResponse<List<MenuEntity>>>(StaticValues.JsonOptions);
                if (apiResponse?.StatusCode == 200)
                {
                    return apiResponse;
                }
                return apiResponse ?? new ApiResponse<List<MenuEntity>> { Errors = "Invalid API response" };
            }
            catch (Exception ex)
            {
                return default;
            }
            finally
            {
                IsLoading = false;
            }
        }
    }
}